package com.hardroid.workload.ui;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.fragment.ObsvControlFragment;
import com.hardroid.workload.ui.fragment.SheetFragment;
import com.hardroid.workload.utils.IObsvFragmentReplacable;
import com.hardroid.workload.utils.Singleton;

public class SheetActivity extends Activity implements IObsvFragmentReplacable {

	public SheetFragment sheetFragment;
	public ObsvControlFragment controlFragment;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_sheet);

		Log.i("Sheet Acitivy", "onCreATE");
		controlFragment = (ObsvControlFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_control);
		sheetFragment = (SheetFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_sheet);

		if (sheetFragment == null) {
			if (!Singleton.currentProject.lEmployer.isEmpty()) {
				Log.i("Sheet Acitivy", "onCreate Sheet Instance ");
				try{
					sheetFragment = SheetFragment.createInstance(0,Singleton.currentProject.lEmployer.get(0).sheet.lObsvDay.size()-1);
				}catch(Exception e){
					sheetFragment = SheetFragment.createInstance(0, 0);
				}
				// Execute a transaction, replacing any existing fragment
				// with this one inside the frame.
				Log.i("Sheet Acitivy", "ft . begin");
				FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				ft.replace(R.id.fragment_sheet, sheetFragment);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.commit();
				Log.i("Sheet Acitivy", "ft . commit");
			}
		}
		if (controlFragment!=null){
			controlFragment.setListEmployer(Singleton.currentProject.lEmployer);
		}		
	}

	
	public void replaceFragment(int id, int did) {
		Log.i("Sheet Acitivy", "replace fragment "+id + ","+did);
	
		if (sheetFragment.employerId != id ){
			Log.i("Sheet Acitivy", "employer - replace fragment create instance"+id);
			sheetFragment = SheetFragment.createInstance(id,did);
	
			// Execute a transaction, replacing any existing fragment
			// with this one inside the frame.
			Log.i("Sheet Acitivy", "replace fragment fr begin "+id);
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			ft.replace(R.id.fragment_sheet, sheetFragment);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			ft.commit();
			Log.i("Sheet Acitivy", "replace fragment fr end "+id);
		}else{
			if (sheetFragment.dayId!=did){
				Log.i("Sheet Acitivy", "day - replace fragment create instance"+did);
				sheetFragment = SheetFragment.createInstance(id,did);
		
				// Execute a transaction, replacing any existing fragment
				// with this one inside the frame.
				Log.i("Sheet Acitivy", "replace fragment fr begin "+did);
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				ft.replace(R.id.fragment_sheet, sheetFragment);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				ft.commit();
				Log.i("Sheet Acitivy", "replace fragment fr end "+did);
			}else{
				Log.i("Sheet Acitivy", "no replace");
			}
		}
//		sheetFragment.init();

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Singleton.saveProject(Singleton.currentProject);
	}


	
	
}
